Imports System
Imports System.Runtime.InteropServices
Imports System.Drawing
Imports System.Drawing.Imaging


''' <summary>
''' Provides functions to capture the entire screen, or a particular window, and save it to a file.
''' </summary>
Public Class ScreenCapture
    ''' <summary>
    '''  Creates an Image object containing a screen shot of the entire desktop.
    ''' </summary>
    Public Function CaptureScreen() As Image
        Return CaptureWindow(AeroAPIs.GetDesktopWindow())
    End Function 'CaptureScreen
    ''' <summary>
    ''' Creates an Image object containing a screen shot of a specific window.
    ''' </summary>
    Public Function CaptureWindow(ByVal handle As IntPtr) As Image
        Dim SRCCOPY As Integer = &HCC0020
        ' get te hDC of the target window
        Dim hdcSrc As IntPtr = AeroAPIs.GetWindowDC(handle)
        ' get the size
        Dim windowRect As New AeroAPIs.RECT
        AeroAPIs.GetWindowRect(handle, windowRect)
        Dim width As Integer = windowRect.right - windowRect.left
        Dim height As Integer = windowRect.bottom - windowRect.top
        ' create a device context we can copy to
        Dim hdcDest As IntPtr = AeroAPIs.CreateCompatibleDC(hdcSrc)
        ' create a bitmap we can copy it to,
        ' using GetDeviceCaps to get the width/height
        Dim hBitmap As IntPtr = GDI32.CreateCompatibleBitmap(hdcSrc, width, height)
        ' select the bitmap object
        Dim hOld As IntPtr = AeroAPIs.SelectObject(hdcDest, hBitmap)
        ' bitblt over
        AeroAPIs.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, SRCCOPY)
        ' restore selection
        AeroAPIs.SelectObject(hdcDest, hOld)
        ' clean up 
        AeroAPIs.DeleteDC(hdcDest)
        AeroAPIs.ReleaseDC(handle, hdcSrc)

        ' get a .NET image object for it
        Dim img As Image = Image.FromHbitmap(hBitmap)
        ' free up the Bitmap object
        AeroAPIs.DeleteObject(hBitmap)

        Return img
    End Function 'CaptureWindow
    ''' <summary>
    ''' Captures a screen shot of a specific window, and saves it to a file.
    ''' </summary>
    Public Sub CaptureWindowToFile(ByVal handle As IntPtr, ByVal filename As String, ByVal format As ImageFormat)
        Dim img As Image = CaptureWindow(handle)
        img.Save(filename, format)
    End Sub 'CaptureWindowToFile

    ''' <summary>
    ''' Captures a screen shot of the entire desktop, and saves it to a file.
    ''' </summary>
    Public Sub CaptureScreenToFile(ByVal filename As String, ByVal format As ImageFormat)
        Dim img As Image = CaptureScreen()
        img.Save(filename, format)
    End Sub 'CaptureScreenToFile
    ''' <summary>
    ''' Creates an Image object containing a screen shot of a specific area of screen.
    ''' </summary>
    Public Function CaptureDeskTopRectangle(ByVal CapRect As Rectangle, ByVal CapRectWidth As Integer, ByVal CapRectHeight As Integer) As Bitmap
        '/ Returns BitMap of the region of the desktop, similar to CaptureWindow, but can be used to 
        '/ create a snapshot of the desktop when no handle is present, by passing in a rectangle 
        '/ Grabs snapshot of entire desktop, then crops it using the passed in rectangle's coordinates
        Dim SC As New ScreenCapture
        Dim bmpImage As New Bitmap(SC.CaptureScreen)
        Dim bmpCrop As New Bitmap(CapRectWidth, CapRectHeight, bmpImage.PixelFormat)
        Dim recCrop As New Rectangle(CapRect.X, CapRect.Y, CapRectWidth, CapRectHeight)
        Dim gphCrop As Graphics = Graphics.FromImage(bmpCrop)
        Dim recDest As New Rectangle(0, 0, CapRectWidth, CapRectHeight)
        gphCrop.DrawImage(bmpImage, recDest, recCrop.X, recCrop.Y, recCrop.Width, _
          recCrop.Height, GraphicsUnit.Pixel)
        Return bmpCrop
    End Function
    '/ Helper class containing Gdi32 API functions
    Private Class GDI32
        Public SRCCOPY As Integer = &HCC0020

        Declare Function CreateCompatibleBitmap Lib "gdi32.dll" ( _
            ByVal hdc As IntPtr, _
            ByVal nWidth As Int32, _
            ByVal nHeight As Int32) As IntPtr
    End Class 'GDI32
End Class 'ScreenCapture 
